const orderModel = require("../model/orderModel");

module.exports.find = async function (params, pagination) {
    let { current, pageSize } = pagination;

    let total = await orderModel.countDocuments(params);

    let data = await orderModel.find(params).populate({ path: "user" }).populate({ path: "itemInfo" }).limit(pageSize).skip((current - 1) * pageSize);
    return {
        current, pageSize, total, rows: data
    }
}

module.exports.findAll = async function (params) {
    return await orderModel.find(params).populate({ path: "user" }).populate({ path: "itemInfo" });
}

module.exports.insert = async function (params) {
    return await orderModel.create(params);
}

module.exports.findById = async function (id) {
    return await orderModel.findById(id).populate({ path: "user" }).populate({ path: "itemInfo" });
}

module.exports.update = async function (params) {
    return await orderModel.updateOne({ _id: params.id }, params);
}

module.exports.remove = async function (id) {
    return await orderModel.deleteOne({ _id: id });
}